package com.project.springboot.excel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

// 定义一个线程类来处理数据块的写入
class ExcelWriterTask implements Callable<Void> {
    private final List<List<String>> data;
    private final Workbook workbook;
    private final String sheetName;

    public ExcelWriterTask(List<List<String>> data, Workbook workbook, String sheetName) {
        this.data = data;
        this.workbook = workbook;
        this.sheetName = sheetName;
    }

    @Override
    public Void call() {
        synchronized (ExcelWriterTask.class) {
            Sheet sheet = workbook.createSheet(sheetName);
            int rowNum = 0;
            for (List<String> rowData : data) {
                Row row = sheet.createRow(rowNum++);
                int colNum = 0;
                for (String cellData : rowData) {
                    Cell cell = row.createCell(colNum++);
                    cell.setCellValue(cellData);
                }
            }
            return null;
        }
    }
}

public class ExcelExportExample {
    public static void main(String[] args) {
        // 模拟大量数据
        List<List<String>> allData = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            List<String> row = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                row.add("Data " + i + "-" + j);
            }
            allData.add(row);
        }

        // 定义线程池
        int threadCount = 4;
        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        List<Future<Void>> futures = new ArrayList<>();

        Workbook workbook = new XSSFWorkbook();

        // 划分数据并提交任务
        int chunkSize = allData.size() / threadCount;
        for (int i = 0; i < threadCount; i++) {
            int start = i * chunkSize;
            int end = (i == threadCount - 1) ? allData.size() : (i + 1) * chunkSize;
            List<List<String>> chunk = allData.subList(start, end);
            ExcelWriterTask task = new ExcelWriterTask(chunk, workbook, "Sheet" + (i + 1));
            futures.add(executor.submit(task));
        }

        // 等待所有任务完成
        for (Future<Void> future : futures) {
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        // 关闭线程池
        executor.shutdown();

        // 保存 Excel 文件
        try (FileOutputStream fileOut = new FileOutputStream("多线程导出.xlsx")) {
            workbook.write(fileOut);
            System.out.println("Excel 文件导出成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}